﻿﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Linq;
using CMS;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;

public partial class Login : System.Web.UI.Page
{
    public string Message = "";
    public CMS.Functions fn = new CMS.Functions();

    protected void Page_Init(object sender, EventArgs e)
    {
        if (Session["RegisteredUsername"] != null)
        {
            txtUserName.Text = Session["RegisteredUsername"].ToString();
        }
    }

    void DropDownAdministration()
    {
        if (!IsPostBack)
        {
            DataTable ADM = CMS.StoredProcedureValues.VW_Administration(null, null, null, true, null, null);
            dropAdministration.DataSource = ADM;
            dropAdministration.DataTextField = "ADM_NAME";
            dropAdministration.DataValueField = "ADM_ID";
            AdministrationAv.Visible = ADM.Rows.Count > 0;
            dropAdministration.DataBind();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ((Content)Master).MetaKeywords = "Login, CMS, NextGen";
        ((Content)Master).MetaTags = "";
        ((Content)Master).PageTitle = "Login - NextGen";

        DropDownAdministration();

        if (Request.QueryString["ClearSession"] != null && Request.QueryString["ClearSession"].ToString() == "1")
        {
            ((Content)Master).Administratie = null;
            ((Content)Master).UserName = null;
            CMS.Functions.VerifyLogin.Valid = false;
            ((Content)Master).Logintitle = "<div id=\"LoginHeaderText\">CMS - NEXT-GEN</div>";
            Request.Cookies.Remove(CMS.Functions.UserID.ToString());
            fn.LogOut();
        }
    }

    void IfLoginIsValid()
    {
        var usr = CMS.Users.GetUsers(null, txtUserName.Text, null, null, Convert.ToInt64(dropAdministration.SelectedItem.Value), null, null, null).FirstOrDefault();
        CMS.Functions.Username = txtUserName.Text;
        CMS.Functions.Administrationname = dropAdministration.SelectedItem.Text;
        CMS.Functions.AdministrationID = Convert.ToInt64(dropAdministration.SelectedItem.Value);
        CMS.Functions.MakeLoginCookie(usr.USR_ID.ToString());
        CMS.Functions.UserLastLogin(CMD.LastLogin, usr.USR_ID, usr.ADM_ID);
        CMS.Functions.UserID = usr.USR_ID;
        Response.Redirect("Page.aspx");
    }


    protected void BtnRegister_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }

    public Boolean ValidPassword;
    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        if (ConfigurationManager.AppSettings["InDevelopment"].ToString() == "True")
        {
            txtUserName.Text = "Admin";
            txtUserWachtwoord.Text = "123456";
            dropAdministration.SelectedItem.Value = "1";
            dropAdministration.SelectedItem.Text = "Development";
        }

        ValidPassword = CMS.Functions.VerifyLogin.Verify(txtUserName.Text, txtUserWachtwoord.Text, Convert.ToInt64(dropAdministration.SelectedItem.Value), dropAdministration.SelectedItem.Text, "Your credentials are wrong !");

        if (!String.IsNullOrEmpty(txtUserName.Text) && !String.IsNullOrEmpty(txtUserWachtwoord.Text))
        {

            if (ValidPassword)
            {
                IfLoginIsValid();
            }
            else
            {
                ((Content)Master).Jscript = CMS.Functions.IsError;
            }
        }
        else
        {
            ((Content)Master).Jscript = CMS.Functions.NormalPopup("Please enter required fields with <em>*</em> !", System.Drawing.KnownColor.Black, "$('.Required').css('border', '1px solid White').css('color','White').css('background-color','Orange');");
        }
    }
}
